package com.cpit.qualitycontrol.service.impl;

import com.cpit.platform.common.constant.SystemConstant;
import com.cpit.platform.common.entity.TableDataQueryCondition;
import com.cpit.platform.common.entity.TableJsonData;
import com.cpit.platform.common.utils.BeanUtil;
import com.cpit.platform.log.LogOperator;
import com.cpit.qualitycontrol.dao.CooperatorStafferRepository;
import com.cpit.qualitycontrol.entity.CooperatorStaffer;
import com.cpit.qualitycontrol.service.CooperatorStafferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Hulunliang
 * @date 2017/11/4.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CooperatorStafferServiceImpl implements CooperatorStafferService {
    private final String loggerName = this.getClass().getName();

    @Autowired
    private CooperatorStafferRepository cooperatorStafferRepository = null;

    @Override
    public CooperatorStaffer findById(Long id) {
        return this.cooperatorStafferRepository.findOne(id);
    }

    @Override
    public List<CooperatorStaffer> findAll() {
        return this.cooperatorStafferRepository.findAll();
    }

    @Override
    public List<CooperatorStaffer> findByCondition(int page, int size, String order, String orderBy, String name, String companyName) {
        if(orderBy == null || orderBy.length() == 0) {
            orderBy = "id";
        }
        Sort.Direction der = order.equals(SystemConstant.DIRECTION_ASC) ? Sort.Direction.ASC : Sort.Direction.DESC;
        PageRequest request = new PageRequest(page - 1, size, new Sort(der, orderBy));
        Page<CooperatorStaffer> pg = this.cooperatorStafferRepository.findByNameContainingAndCompanyNameContaining(name, companyName, request);

        return pg.getContent();
    }

    @Override
    public CooperatorStaffer create(CooperatorStaffer cooperatorStaffer) throws Exception {
        if (cooperatorStaffer == null) {
            throw new Exception("参数为空");
        }
        if (cooperatorStaffer.getId() != null && cooperatorStaffer.getId()>0) {
            LogOperator.info(loggerName, "合作方人员ID已赋值");
            cooperatorStaffer.setId(0L);
        }
//        this.checkUniqueConstraint(cooperatorStaffer);

        return this.cooperatorStafferRepository.save(cooperatorStaffer);
    }

    @Override
    public CooperatorStaffer modify(CooperatorStaffer cooperatorStaffer) throws Exception {
        if (cooperatorStaffer == null) {
            throw new Exception("参数为空");
        }
        CooperatorStaffer cooperatorStaffer1 = this.cooperatorStafferRepository.findOne(cooperatorStaffer.getId());
        if(cooperatorStaffer1 == null) {
            LogOperator.info(loggerName, "修改的合作方人员不存在:" + cooperatorStaffer);
            throw new Exception("修改的合作方人员不存在");
        }
//        this.checkUniqueConstraint(cooperatorStaffer);
        return this.cooperatorStafferRepository.save(cooperatorStaffer);
    }

    @Override
    public void delete(CooperatorStaffer cooperatorStaffer) throws Exception {
        if (cooperatorStaffer == null) {
            throw new Exception("参数为空");
        }
        this.delete(cooperatorStaffer.getId());
    }

    @Override
    public void delete(Long id) throws Exception {
        CooperatorStaffer cooperatorStaffer = this.cooperatorStafferRepository.findOne(id);
        if(cooperatorStaffer == null) {
            LogOperator.info(loggerName, "删除的合作方人员不存在:" + id);
            throw new Exception("删除的合作方人员不存在");
        }
        this.cooperatorStafferRepository.delete(id);
    }

    @Override
    public TableJsonData findByConditionJqg(TableDataQueryCondition cond) {
        PageRequest request = ServiceUtils.getPageRequestByTableDataQueryCondition(cond);

        String name = BeanUtil.getStringIdentifyNull(cond.getQueryCond().get("name"));
        String companyName = BeanUtil.getStringIdentifyNull(cond.getQueryCond().get("companyName"));
        Page<CooperatorStaffer> pg = this.cooperatorStafferRepository.findByNameContainingAndCompanyNameContaining(name, companyName, request);
        return new TableJsonData(cond, pg.getTotalElements(), pg.getContent());
    }

    private void checkUniqueConstraint(CooperatorStaffer cooperatorStaffer) throws  Exception {
        if(cooperatorStaffer.getMobile()!=null&&!"".equals(cooperatorStaffer.getMobile())){
        CooperatorStaffer cooperatorStaffer1 = this.cooperatorStafferRepository.findByMobile(cooperatorStaffer.getMobile());
        if(cooperatorStaffer1 != null && !cooperatorStaffer.getId().equals(cooperatorStaffer1.getId())) {
            LogOperator.err(loggerName,"手机号已存在:" + cooperatorStaffer.getMobile());
            throw new Exception("手机号已存在");
        }
        }
        if(cooperatorStaffer.getEmail()!=null&&!"".equals(cooperatorStaffer.getEmail())){
            CooperatorStaffer cooperatorStaffer1 = this.cooperatorStafferRepository.findByEmail(cooperatorStaffer.getEmail());
            if(cooperatorStaffer1 != null && !cooperatorStaffer.getId().equals(cooperatorStaffer1.getId())) {
                LogOperator.err(loggerName,"邮箱已存在:" + cooperatorStaffer.getMobile());
                throw new Exception("邮箱已存在");
            }
        }

    }
}
